/*
 * Copyright 2018 dc-square GmbH
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package com.hivemq.extensions.rbac;

import com.hivemq.extensions.rbac.listener.FileAuthenticatorProvider;
import com.hivemq.extensions.rbac.utils.CredentialsValidator;
import com.hivemq.extension.sdk.api.ExtensionMain;
import com.hivemq.extension.sdk.api.annotations.NotNull;
import com.hivemq.extension.sdk.api.parameter.ExtensionStartInput;
import com.hivemq.extension.sdk.api.parameter.ExtensionStartOutput;
import com.hivemq.extension.sdk.api.parameter.ExtensionStopInput;
import com.hivemq.extension.sdk.api.parameter.ExtensionStopOutput;
import com.hivemq.extension.sdk.api.services.Services;
import com.hivemq.extensions.rbac.configuration.Configuration;
import com.hivemq.extensions.rbac.configuration.ExtensionConfiguration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;

/**
 * 文件身份验证 main
 */
public class FileAuthMain implements ExtensionMain {

    private static final @NotNull Logger log = LoggerFactory.getLogger(FileAuthMain.class);

    /**
     * 插件启动
     * @param extensionStartInput   插件输入
     * @param extensionStartOutput  插件输出
     */
    @Override
    public void extensionStart(final @NotNull ExtensionStartInput extensionStartInput, final @NotNull ExtensionStartOutput extensionStartOutput) {

        log.info("[插件启动] 授权验证 RBAC.");

        try {
            // 获取扩展信息,获取插件目录
            final File extensionHomeFolder = extensionStartInput.getExtensionInformation().getExtensionHomeFolder();

            // 插件配置文件
            final ExtensionConfiguration extensionConfiguration = new ExtensionConfiguration(extensionHomeFolder);
            // 配置
            final Configuration configuration = new Configuration(extensionHomeFolder, Services.extensionExecutorService(),
                    extensionConfiguration.getExtensionConfig());
            configuration.init();

            log.info("凭证验证器初始化");
            // 凭据验证器
            final CredentialsValidator credentialsValidator = new CredentialsValidator(configuration,
                    extensionConfiguration.getExtensionConfig(), Services.metricRegistry());
            credentialsValidator.init();

            log.info("注册安全认证授权");
            // 注册安全认证授权
            Services.securityRegistry().setAuthenticatorProvider(new FileAuthenticatorProvider(credentialsValidator));
        } catch (Exception e) {
            log.error("在扩展开始时抛出异常: ", e);
        }

    }

    @Override
    public void extensionStop(final @NotNull ExtensionStopInput extensionStopInput, final @NotNull ExtensionStopOutput extensionStopOutput) {
        log.info("[插件关闭] File RBAC.");
    }



}
